<?php
/**
 * ============================================================================
 * 版权所有 2008-2011 多多网络，并保留所有权利。
 * 网站地址: http://soft.duoduo123.com；
 * ----------------------------------------------------------------------------
 * 这不是一个自由软件！您只能在不用于商业目的的前提下对程序代码进行修改和
 * 使用；不允许对程序代码以任何形式任何目的的再发布。
 * ============================================================================
*/
header("Content-type: text/html; charset=utf-8"); 
include('../comm/config.php');
include_once 'check.php';

function sel_sql($table,$sel_field,$where,$alert=0){
	global $BIAOTOU;
	$sql="select $sel_field from $BIAOTOU$table where $where";
	if($alert==1){
	    echo $sql;
	}
	$query=mysql_query($sql);
	if($query){
		while($row=mysql_fetch_array($query)){
			if(strpos($sel_field,",") or $sel_field=="*"){
			    $arr=$row;
			}
			else{
			    $arr=$row[$sel_field];
			}
		}
	}
	return $arr;	
}

$page = !($_GET['page'])?'1':intval($_GET['page']);
$pagesize=200;
$frmnum=($page-1)*$pagesize;

$sql="update ".$BIAOTOU."tradelist set checked=0 where outer_code=0";
mysql_query($sql);

$sql="select * from ".$BIAOTOU."tradelist where length(outer_code)>=8 limit $frmnum,$pagesize";
$query=mysql_query($sql);
while($row=mysql_fetch_array($query)){
    $commission=$row['commission'];
	$trade_id=$row['trade_id'];
	$outer_code=$row['outer_code'];
	$id=$row['Id'];
	$uid=substr($outer_code,8,5);
	$uid=$uid?$uid:0;
	if($uid>0){
		$dduser=sel_sql('user','ddusername,dengji','Id='.$uid);
	    $ddusername=$dduser['ddusername'];
	    $dddengji=$dduser['dengji'];
	    $sql="update ".$BIAOTOU."tradelist set outer_code=".$uid." where trade_id ='".$trade_id."'";
	}
	else{
	    $sql="update ".$BIAOTOU."tradelist set outer_code=".$uid.",checked=0 where trade_id ='".$trade_id."'";
	}
	mysql_query($sql);
	if($ddusername){
		$sj=date('Y-m-d H:i:s');
		$ec=$ec."纠正订单数据，订单号：".$trade_id."<br/>";
	    $fxje=ck_fxje($commission,$dddengji);
		$sql="update ".$BIAOTOU."user set money=money+".$fxje.",dengji=dengji+1 where ddusername ='".$ddusername."'";
		mysql_query($sql);
		//写入返现金额到交易列表
		$sql="update ".$BIAOTOU."tradelist set fxje=".$fxje.",qrsj='".$sj."' where trade_id ='".$trade_id."'";
		mysql_query($sql);
		//用户消息
		$msg_tabao=str_replace("[trade_id]",$trade_id,$msg_tabao);
		$msg_tabao=str_replace("[fxje]",$fxje,$msg_tabao);
		$sql="insert into ".$BIAOTOU."msg (title,content,addtime,see,ddusername,senduser) values ('您获得了新的交易返现！','".$msg_tabao."','$sj',0,'".$ddusername."','网站客服');";
		mysql_query($sql);
		$msg_tabao=str_replace($trade_id,"[trade_id]",$msg_tabao);
		$msg_tabao=str_replace($fxje,"[fxje]",$msg_tabao);
		//用户明细
		$sql="insert into ".$BIAOTOU."mingxi (ddusername,shijian,je,addtime,memo) values ('".$ddusername."','交易返现',".$fxje.",'$sj','交易号".$trade_id."')";
		mysql_query($sql);							
		//取推荐人
		$sql="select tjr from ".$BIAOTOU."user where ddusername='".$ddusername."' and tjr is not NULL and tjr<>''";
		$t=mysql_query($sql);
		$num1 = mysql_num_rows($t);	
		//如果推荐人存在，就进行下面的操作					
		if($num1!=0){   
			$tjrid="";
			while ($tt = mysql_fetch_array($t)){
				$tjrid=$tt["tjr"]; //推广人佣金
				$sql="update ".$BIAOTOU."user set money=money+".round($commission*TGBL,2)." where Id='".$tjrid."'";
				mysql_query($sql);
								
				//取推荐人名
				$sqltjr="select ddusername from ".$BIAOTOU."user where Id='".$tjrid."'";
				$tgrq=mysql_query($sqltjr);
				$tu = mysql_fetch_array($tgrq);
				$tjrusername=$tu["ddusername"]; 	
								
				//推广人明细
				$sql="insert into ".$BIAOTOU."mingxi (ddusername,shijian,je,addtime,memo) values ('".$tjrusername."','推广佣金',".round($commission*TGBL,2).",'$sj','交易人".$ddusername."，交易号".$trade_id."')";
				mysql_query($sql);
				//推广人消息
				$tgje=round($commission*TGBL,2);
				$msg_taobaotuiguang_temp=$msg_taobaotuiguang;
				$msg_taobaotuiguang_temp=str_replace("[ddusername]",$ddusername,$msg_taobaotuiguang_temp);
				$msg_taobaotuiguang_temp=str_replace("[trade_id]",$trade_id,$msg_taobaotuiguang_temp);
				$msg_taobaotuiguang_temp=str_replace("[tgje]",$tgje,$msg_taobaotuiguang_temp);
				$sql="insert into ".$BIAOTOU."msg (title,content,addtime,see,ddusername,senduser) values ('您获得了新的推广佣金！','".$msg_taobaotuiguang_temp."','$sj',0,'".$tjrusername."','网站客服');";
				mysql_query($sql);
			}
		}
	}
}
if($ec){
    echo $ec;
}
else{
    echo '您没有该类型的错误订单';
}
?>